//
//  GroMoreSDKManager.m
//  WDGroMore
//
//  Created by YeJary on 2025/5/15.
//

// GroMoreSDKManager.m
#import "GroMoreSDKManager.h"
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/AdSupport.h>
#import <AFNetworking/AFNetworkReachabilityManager.h>
#import "WDBMS.h"
#import "WDGroMoreBannerManager.h"
#import "WDWDGroMoreRewardVideoManager.h"
#import "WDGroMoreInterstitialManager.h"
#import "WDGroMoreTipManager.h"
#import "WDGroMoreThinkingManager.h"
#import "UMAnalytics.h"
#import "WDKeychainInfoManager.h"
#import "WDGroMoreJosnManager.h"
#import "BUDPrivacyProvider.h"
#import "WDGroMoreTrackManager.h"
#import "WDGroMoreNativeManager.h"
#import "WDGroMoreSplashManager.h"

@interface GroMoreSDKManager ()

@property (nonatomic, assign) BOOL hasNetwork;

@property (nonatomic, assign) BOOL didInit;

@property (nonatomic, copy) NSString *uuid;

@end

@implementation GroMoreSDKManager

+ (instancetype)shared {
    static GroMoreSDKManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[GroMoreSDKManager alloc] init];
    });
    return manager;
}

- (void)initialize {
    [self monitorNetwork];
}

#pragma mark - 网络监听

- (void)monitorNetwork {
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusNotReachable || status == AFNetworkReachabilityStatusUnknown) {
            self.hasNetwork = NO;
            [[WDGroMoreTipManager manager] show];
        } else {
            self.hasNetwork = YES;
            [[WDGroMoreTipManager manager] remove];
            [self initGroMoreWithUuid:[WDGroMorePublic manager].uuid isSuccess:[WDGroMorePublic manager].success];
        }
    }];
    [mgr startMonitoring];
}

#pragma mark - ATT 授权 + 广告 SDK 初始化

- (void)initializeGroMoreSDKWithUUID:(NSString *)uuid isSuccess:(BOOL)isSuccess {
    if ([WDGroMorePublic manager].unityPro) {
            if ([[WDGroMoreDelegateManager manager].delegate respondsToSelector:@selector(WDGroMoreConfig:)]) {
                [self initGroMoreUnityProWithUuid:uuid isSuccess:isSuccess];
            }
        } else {
            [self initGroMoreWithUuid:uuid isSuccess:isSuccess];
        }
}

-(void)initGroMoreUnityProWithUuid:(NSString *)uuid isSuccess:(BOOL)isSuccess{
    if ([WDGroMoreDelegateManager manager].delegate && [[WDGroMoreDelegateManager manager].delegate respondsToSelector:@selector(WDGroMoreConfig:)])
    {
        if (isSuccess && uuid.length > 0)
        {
            [[WDGroMoreDelegateManager manager].delegate WDGroMoreConfig:uuid];
        }
        else
        {
            [[WDGroMoreDelegateManager manager].delegate WDGroMoreConfig:[[WDKeychainInfoManager manager] WDKeychainGetUUID]];
        }
    }
}


-(void)initGroMoreWithUuid:(NSString *)uuid isSuccess:(BOOL)isSuccess{
    if(isSuccess&&uuid&& uuid.length > 0){
        [self setupBUAdSDKWithUUID:uuid];
  
    }else{
        [self setupBUAdSDKWithUUID:[[WDKeychainInfoManager manager] WDKeychainGetUUID]];

    }
    
    
}

- (void)setupBUAdSDKWithUUID:(NSString *)uuid {
    
    [WDGroMoreTrackManager track:BMS_DTYPE_AD_ACTION params:@{BMS_PARAM_WDS:BMS_WDS_AD_APPID} dType:BMS_DTYPE_AD_ACTION desc:@"" options:[WDGroMoreTrackManager trackOptionsOnlySendBMS]];
    
    BUAdSDKConfiguration *config = [BUAdSDKConfiguration configuration];
    config.appID = [WDGroMoreJosnManager GroMoreAppidForSDKConfigFile];
    config.privacyProvider = [[BUDPrivacyProvider alloc] init];
    config.debugLog = @(self.isDebug);
    config.SDKDEBUG = self.isDebug;
    config.unityDeveloper = self.isUnity;
    config.useMediation = YES;
    // 是否禁止CAID
    config.mediation.forbiddenIDFA = @(0);
    // 是否限制个性化广告
    config.mediation.limitPersonalAds = @(0);
    // 是否限制程序化广告
    config.mediation.limitPersonalAds = @(0);
    // 主题模式
    config.themeStatus = @(BUAdSDKThemeStatus_Normal);
    // 自定义IDFA

    [BUAdSDKManager startWithAsyncCompletionHandler:^(BOOL success, NSError * _Nullable error) {
        if (success) {
            [[WDWDGroMoreRewardVideoManager manager] createRewardedAdWithPlacementId:[WDGroMoreJosnManager GroMoreRewardAdIdForSDKConfigFile] userId:uuid];
            [[WDGroMoreInterstitialManager manager] createInterstitialAdWithPlacementId:[WDGroMoreJosnManager GroMoreInterstitialIdForSDKConfigFile] userId:uuid];
            [[WDGroMoreBannerManager manager] createBannerAdWithPlacementId:[WDGroMoreJosnManager GroMoreBannerAdIdForSDKConfigFile] userId:uuid];
            [[WDGroMoreNativeManager manager]loadNativeAdWithPlacementId:[WDGroMoreJosnManager GroMoreNativeIdForSDKConfigFile] userId:uuid];
//            [[WDGroMoreSplashManager manager]createSplashAdWithPlacementId:[WDGroMoreJosnManager GroMoreSplashIdForSDKConfigFile] userId:uuid];
        } else {
            NSLog(@"[GroMoreSDK] 初始化失败: %@", error);
        }
    }];
}

#pragma mark - 广告展示

- (void)showRewardVideo {
    [[WDWDGroMoreRewardVideoManager manager] showRewardedAd];
}

- (void)showInterstitial {
    [[WDGroMoreInterstitialManager manager] showInterstitialdAd];
}

- (void)showBannerAtTop:(BOOL)isTop {
    [[WDGroMoreBannerManager manager] showBannerAdWithTop:isTop];
}

- (void)closeBanner {
    [[WDGroMoreBannerManager manager] closeBannerAd];
}

- (void)showSplashAd {
//    [[WDGroMoreSplashManager manager]showSplashAd];
}

- (void)tryShowSplashAdWithLaunch {
    [[WDGroMoreSplashManager manager]tryShowSplashAdWithLaunch];
}

- (void)showNativeAd {
    [WDGroMoreNativeManager showNativeAd];
}

- (void)showNativeAdAtPosition:(NSInteger)position yOffset:(CGFloat)yOffset {
    [WDGroMoreNativeManager showNativeAdAtPosition:position yOffset:yOffset];
}

- (void)removeNativeAd {
    [WDGroMoreNativeManager removeNativeAd];
}

#pragma mark - 事件上报

- (void)trackEvent:(NSString *)event params:(NSDictionary *)params {
    if (event.length == 0) return;
    [WDGroMoreTrackManager track:event params:params dType:@"" desc:@"" options:@{}];
}

@end

